package com.tuishan.myapplication.Adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ToastUtils;
import com.facebook.drawee.view.SimpleDraweeView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tuishan.myapplication.Custom.BuyDialog;
import com.tuishan.myapplication.Custom.LoadingView;
import com.tuishan.myapplication.Model.PageNumModel;
import com.tuishan.myapplication.Model.ProductModel;
import com.tuishan.myapplication.R;
import com.tuishan.myapplication.Servers.BaseCallback;
import com.tuishan.myapplication.Servers.OkHttpHelper;
import com.tuishan.myapplication.Servers.UrlRouter;

import java.util.List;
import java.util.WeakHashMap;

import okhttp3.Request;
import okhttp3.Response;

/**
 * <pre>
 *     time   : 2021/04/18
 *     desc   :
 *     version: 1.0
 * </pre>
 */

public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.ViewHolder> {

    private List<ProductModel> mProductList;

    //1生活 2学习 3服饰 4食品 5家具 6其他
    String[] TypeList = new String[]{
            "", "生活", "学习", "服饰", "食品", "家具", "其他"
    };
    //1全新 2几乎全新 3轻微使用 4正常使用 5明显使用
    String[] UsageList = new String[]{
            "", "全新", "几乎全新", "轻微使用", "正常使用", "明显使用"
    };
    LoadingView Loading;
    Context mContext;
    boolean LeftVis;

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView title_name, title_num, title_message, title_use, title_type;
        Button home_collection_btn, home_buy_btn;
        SimpleDraweeView product_image;

        public ViewHolder(View view, boolean type) {
            super(view);
            title_name = view.findViewById(R.id.title_name);
            title_num = view.findViewById(R.id.title_num);
            title_use = view.findViewById(R.id.title_use);
            title_type = view.findViewById(R.id.title_type);
            title_message = view.findViewById(R.id.title_message);
            home_collection_btn = view.findViewById(R.id.home_collection_btn);
            home_buy_btn = view.findViewById(R.id.home_buy_btn);
            product_image = view.findViewById(R.id.product_image);
            if (type) {
                home_collection_btn.setVisibility(View.VISIBLE);
            } else {
                home_collection_btn.setVisibility(View.GONE);
            }
        }

    }

    public HomeAdapter(List<ProductModel> productModelList, LoadingView arg1, Context arg2) {
        this.mProductList = productModelList;
        this.Loading = arg1;
        this.mContext = arg2;
        this.LeftVis = true;
    }

    public HomeAdapter(List<ProductModel> productModelList, LoadingView arg1, Context arg2,
                       boolean leftVis) {
        this.mProductList = productModelList;
        this.Loading = arg1;
        this.mContext = arg2;
        this.LeftVis = leftVis;
    }

    @NonNull
    @Override
    public HomeAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.items_home_product, parent, false);
        ViewHolder holder = new ViewHolder(view, LeftVis);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull HomeAdapter.ViewHolder holder, int position) {
        ProductModel productModel = mProductList.get(position);
        holder.product_image.setImageURI(productModel.getPhoto());
        holder.title_name.setText("名字：" + productModel.getName());
        holder.title_num.setText("价格：" + productModel.getAccount());
        holder.title_message.setText("描述：" + productModel.getRemarks());
        holder.title_use.setText("新旧：" + UsageList[productModel.getUsage()]);
        holder.title_type.setText("类型：" + TypeList[productModel.getClassType()]);
        holder.home_collection_btn.setOnClickListener(view -> CollectionTrans(productModel.getId()));
        holder.home_buy_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BuyDialog redDialog = new BuyDialog(mContext, Loading, productModel.getId());
                redDialog.show();
            }
        });

    }

    @Override
    public int getItemCount() {
        return mProductList.size();
    }

    //购买
    private void BuyTrans(int productId, String moneyPwd, String remarks) {
        Loading.show();
        WeakHashMap<String, Object> objectMap = new WeakHashMap<>();
        objectMap.put("productId", productId);
        objectMap.put("moneyPwd", moneyPwd);
        objectMap.put("remarks", remarks);
        OkHttpHelper mHttpHelper = OkHttpHelper.getInstance();
        mHttpHelper.post(UrlRouter.BuyCollectionTransactUrl, String.class, objectMap, new BaseCallback() {
            @Override
            public void onFailure(Request request, Exception e) {
                Loading.dismiss();
            }

            @Override
            public void onSuccess(Response response, Object arg1) {
                Loading.dismiss();
                ToastUtils.showShort("购买成功成功");
            }

            @Override
            public void onFalse(Response response, String err_msg) {
                Loading.dismiss();

            }

            @Override
            public void onError(Response response, int errorCode, Exception e) {
                Loading.dismiss();
            }
        });
    }

    //收藏
    private void CollectionTrans(int productId) {
        Loading.show();
        WeakHashMap<String, Object> objectMap = new WeakHashMap<>();
        objectMap.put("productId", productId);
        OkHttpHelper mHttpHelper = OkHttpHelper.getInstance();
        mHttpHelper.post(UrlRouter.CollectionTransactUrl, String.class, objectMap, new BaseCallback() {
            @Override
            public void onFailure(Request request, Exception e) {
                Loading.dismiss();
            }

            @Override
            public void onSuccess(Response response, Object arg1) {
                Loading.dismiss();
                ToastUtils.showShort("收藏成功");
            }

            @Override
            public void onFalse(Response response, String err_msg) {
                Loading.dismiss();

            }

            @Override
            public void onError(Response response, int errorCode, Exception e) {
                Loading.dismiss();
            }
        });
    }
}
